package com.imessage.action.no.usephone;

import com.bean.BusinessGroup;
import com.bean.BusinessUsePhone;
import com.imessage.comm.Constants;
import com.imessage.type.BaseState;
import com.imessage.vo.UsePhoneVo;
import com.imessage.web.Page;
import io.ebean.Expr;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/admin/usephone")
public class UsephoneAction extends UsephoneBaseAction {

	@RequestMapping(value = "")
	public Object run(UsePhoneVo usephoneVo) {
		List<Expression> expressionList=new ArrayList<Expression>();
		setExpressionList(expressionList, usephoneVo);
		Page<BusinessUsePhone> page=baseService.findPage(BusinessUsePhone.class, expressionList,
				null, getOrder(usephoneVo.getOrders()), usephoneVo.getPageCurrent(),
				usephoneVo.getPageSize());
		return page;
	}

	@RequestMapping(value = "initpage")
	public ModelAndView initpage(
			@RequestParam(name="req_id",required = true)String id){
		ModelAndView modelAndView = new ModelAndView();
		BusinessUsePhone usePhone=new BusinessUsePhone();
		if(StringUtils.isNotBlank(id)){
			usePhone=baseService.findById(BusinessUsePhone.class, id);
			modelAndView.setViewName("admin/no/usephone/modify");
		}else{
			modelAndView.setViewName("admin/no/usephone/add");
		}
		setModelAndView(modelAndView,usePhone);
		return modelAndView;
	}



	@RequestMapping(value = "add")
	public Object add(UsePhoneVo usephoneVo) {
		BusinessUsePhone usePhone=new BusinessUsePhone();
		setUsePhone(usephoneVo, usePhone);
		int state=baseService.saveObject(usePhone);
		if(state==Constants.STATE_OPERATOR_SUCC){
			return getJsonForSucc("", datagrid);
		}
		return getJsonForErr("", datagrid);
	}


	@RequestMapping(value = "modify")
	public Object modify(UsePhoneVo usephoneVo) {
		BusinessUsePhone usePhone=baseService.findById(BusinessUsePhone.class, usephoneVo.getId());
		setUsePhone(usephoneVo, usePhone);
		int state=baseService.updateObject(usePhone);
		if(state==Constants.STATE_OPERATOR_SUCC){
			return usePhone;
		}
		return getJsonForErr("", datagrid);
	}
	@RequestMapping(value = "del")
	public Object del(@RequestParam(name="id",required = true)String ids) {
		int state=Constants.STATE_OPERATOR_LOST;
		for(String id:ids.split(",")){
			BusinessUsePhone usePhone=baseService.findById(BusinessUsePhone.class,id);
			state=baseService.delObject(usePhone);
		}
		if(state==Constants.STATE_OPERATOR_SUCC){
			return getJsonForSucc("", datagrid);
		}
		return getJsonForErr("", datagrid);
	}


	@RequestMapping(value = "/initimp")
	public ModelAndView initimp() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("action", "/admin/usephone/imp");
		modelAndView.addObject("templeturl","/static/file/usephone.xlsx");
		modelAndView.setViewName("admin/comm/imp");
		return modelAndView;
	}


	@RequestMapping(value = "/imp")
	public Object imp(MultipartFile upfile){
		String filePath= fileUpload(upfile);
		if(StringUtils.isBlank(filePath)){
			return getJsonForErr("上传文件为空或者格式不正确,只支持xls、xlsx格式", datagrid);
		}
		if(saveExcel(filePath)){
			return getJsonForSucc("上传成功", datagrid);
		}
		return getJsonForErr("上传失败", datagrid);
	}

}
